package cn.skycity.common.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.ISysClientService;
import cn.skycity.framework.core.model.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skycity.common.excel.utils.ExcelUtil;
import com.skycity.common.log.annotation.Log;
import com.skycity.common.log.enums.BusinessType;
import com.skycity.framework.uaa.pojo.dto.SysClientBo;
import com.skycity.framework.uaa.pojo.entity.SysClient;
import com.skycity.framework.uaa.pojo.vo.SysClientVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户端管理
 *
 * @author Michelle.Chung
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/client")
public class SysClientController {

    private final ISysClientService sysClientService;

    /**
     * 查询客户端管理列表
     */
//    @SaCheckPermission("system:client:list")
    @GetMapping("/list")
    public Result<Page<SysClientVo>> pageList(Page<SysClient> pageVo, SysClient sysConfig) {
        LambdaQueryWrapper<SysClient> queryWrapper = new LambdaQueryWrapper<SysClient>()
                .like(StrUtil.isNotBlank(sysConfig.getClientKey()), SysClient::getClientKey, sysConfig.getClientKey())
                .eq(ObjUtil.isNotNull(sysConfig.getStatus()),SysClient::getStatus,sysConfig.getStatus())
                .orderByDesc(SysClient::getClientId);
        Page<SysClient> result = sysClientService.page(pageVo, queryWrapper);
        Page<SysClientVo> pageResult = new Page<>();
        pageResult.setTotal(result.getTotal());
        List<SysClientVo> sysClientVos = BeanUtil.copyToList(result.getRecords(), SysClientVo.class);
        sysClientVos.forEach(sysClientVo -> {
            sysClientVo.setGrantTypeList(List.of(sysClientVo.getGrantType().split(",")));
        });
        pageResult.setRecords(sysClientVos);
        return Result.success(pageResult);
    }

    /**
     * 导出客户端管理列表
     */
//    @SaCheckPermission("system:client:export")
    @Log(title = "客户端管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysClientBo bo, HttpServletResponse response) {
        List<SysClientVo> list = sysClientService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户端管理", SysClientVo.class, response);
    }

    /**
     * 获取客户端管理详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:client:query")
    @GetMapping("/{id}")
    public Result<SysClientVo> getInfo(@PathVariable Long id) {
        return Result.success(sysClientService.queryById(id));
    }

    /**
     * 新增客户端管理
     */
//    @SaCheckPermission("system:client:add")
    @Log(title = "客户端管理", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
    @PostMapping()
    public Result<Void> add(@RequestBody SysClientBo bo) {
        sysClientService.insertByBo(bo);
        return Result.success();
    }

    /**
     * 修改客户端管理
     */
//    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
    @PutMapping()
    public Result<Void> edit(@RequestBody SysClientBo bo) {
        sysClientService.updateByBo(bo);
        return Result.success();
    }

    /**
     * 状态修改
     */
//    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public Result<Void> changeStatus(@RequestBody SysClientBo bo) {
        sysClientService.updateUserStatus(bo.getClientId(), bo.getStatus());
        return Result.success();
    }

    /**
     * 删除客户端管理
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:client:remove")
    @Log(title = "客户端管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Void> remove(@PathVariable Long[] ids) {
        sysClientService.deleteWithValidByIds(List.of(ids), true);
        return Result.success();
    }
}
